package com.constructiveproof.example


import com.constructiveproof.example.{ScentryauthdemoStack}
import com.constructiveproof.example.auth.AuthenticationSupport

class SessionsController extends ScentryauthdemoStack with AuthenticationSupport {

  before("/new") {
    logger.info("SessionsController: 检查是否运行RememberMeStrategy（记住密码策略）： " + !isAuthenticated)
    
    if(!isAuthenticated) { //如果没有运行记住密码
      scentry.authenticate("RememberMe") //执行这个策略
    }
  }

  get("/new") {
    if (isAuthenticated) redirect("/")

    contentType="text/html"
    ssp("/sessions/new")
  }

  post("/") {
    logger.info("scentry.authenticate()之前")
    scentry.authenticate()
    logger.info("scentry.authenticate()之后")

    if (isAuthenticated) {
      redirect("/")
    }else{
      redirect("/sessions/new")
    }
  }

  //在真是的项目中，绝对不会这么做。状态改变绝对不应该使用GET方式
  //但是测试时更容易展示登出代码
  get("/logout") {
    scentry.logout()
    redirect("/")
  }

}